package com.lion.tag.panel;

import java.io.IOException;

import javax.servlet.jsp.JspException;

import com.lion.tag.LionBaseTag;
import com.lion.tag.util.AppendUtil;

public class Dialog extends LionBaseTag {
	private static final long serialVersionUID = 4630633974996264800L;
	private String title;
	private String icon;
	private String toolbar;
	private String buttons;
	private String closed;
	private String modal;

	/**
	 * 实现对话框
	 */
	@Override
	public int doStartTag() throws JspException {
		StringBuilder sb = new StringBuilder(200);
		sb.append("<div ");
		sb = AppendUtil.addAttribute(sb, "id", id);
		sb = AppendUtil.addAttribute(sb, "title", title);
		sb = AppendUtil.addAttribute(sb, "class", "easyui-dialog");
		sb = AppendUtil.addAttribute(sb, "style", style);
		sb.append("data-options=\"");
		int hasData = 0;
		if (icon != null && !icon.equals("")) {
			sb.append("iconCls:'");
			sb.append(icon);
			sb.append("'");
			hasData = 1;
		}
		if (toolbar != null && !toolbar.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			} else {
				hasData = 1;
			}
			if(toolbar.charAt(0)!='#'){
				toolbar ="#"+toolbar;
			}
			sb.append("toolbar:'");
			sb.append(toolbar);
			sb.append("'");
		}
		if (buttons != null && !buttons.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			} else {
				hasData = 1;
			}
			if(buttons.charAt(0)!='#'){
				buttons ="#"+buttons;
			}
			sb.append("buttons:'");
			sb.append(buttons);
			sb.append("'");
		}
		if (closed != null && !closed.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			} else {
				hasData = 1;
			}
			sb.append("closed:");
			sb.append(closed);
		}
		if (modal != null && !modal.equals("")) {
			if (hasData == 1) {
				sb.append(",");
			} else {
				hasData = 1;
			}
			sb.append("modal:");
			sb.append(modal);
		}
		sb.append("\" >");
		try {
			pageContext.getOut().write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;// 表示标签之间的内容被正常执行
	}

	/**  
	 *  doEndTag：当JSP容器遇到自定义标签的结束标志，就会调用doEndTag()方法。doEndTag()方法也返回一个整数值，用来决定程序后续流程。  
	 *  A.Tag.SKIP_PAGE：表示立刻停止执行网页，网页上未处理的静态内容和JSP程序均被忽略任何已有的输出内容立刻返回到客户的浏览器上。  
	 *  B.Tag.EVAL_PAGE：表示按照正常的流程继续执行JSP网页  
	 */
	@Override
	public int doEndTag() throws JspException {
		try {
			pageContext.getOut().write("</div>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title
	 *            the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the icon
	 */
	public String getIcon() {
		return icon;
	}

	/**
	 * @param icon
	 *            the icon to set
	 */
	public void setIcon(String icon) {
		this.icon = icon;
	}

	/**
	 * @return the toolbar
	 */
	public String getToolbar() {
		return toolbar;
	}

	/**
	 * @param toolbar
	 *            the toolbar to set
	 */
	public void setToolbar(String toolbar) {
		this.toolbar = toolbar;
	}

	/**
	 * @return the buttons
	 */
	public String getButtons() {
		return buttons;
	}

	/**
	 * @param buttons
	 *            the buttons to set
	 */
	public void setButtons(String buttons) {
		this.buttons = buttons;
	}

	public String getClosed() {
		return closed;
	}

	public void setClosed(String closed) {
		this.closed = closed;
	}

	public String getModal() {
		return modal;
	}

	public void setModal(String modal) {
		this.modal = modal;
	}

}
